import { message } from "antd";
import { ResponseData, MenuResponse, MenuList, LoginApi, DocInfoResponse } from "@/types"
import { formatMenu } from "@/utils";
type MockDataType = {
  "/getMenu/{productType}": MenuResponse
  "/login": LoginApi
  "/getDocDetail/{doc_id}":DocInfoResponse
  "/evaluate":ResponseData
  "/editDocContent":ResponseData
  [key: string]: ResponseData | MenuList | MenuResponse | DocInfoResponse
}

let menu: MenuList = [
 
  {
    menuId: 2,
    [MENU_TITLE]: "计算介绍",
    [MENU_KEY]: "introduce",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 2,
  },
  {
    menuId: 6,
    [MENU_TITLE]: "快速开始",
    [MENU_KEY]: "start",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 66,
  },
  {
    menuId: 3,
    [MENU_TITLE]: "产品介绍",
    [MENU_KEY]: "productIntroduction",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 3,
  },
  {
    menuId: 31,
    [MENU_TITLE]: "平台概述",
    [MENU_KEY]: "PlatformOverview",
    [MENU_PARENTKEY]: "productIntroduction",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 31,
  },
  {
    menuId: 32,
    [MENU_TITLE]: "快速使用",
    [MENU_KEY]: "QuickUse",
    [MENU_PARENTKEY]: "productIntroduction",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 32,
  },
  {
    menuId: 33,
    [MENU_TITLE]: "计算使用",
    [MENU_KEY]: "CalculationUse",
    [MENU_PARENTKEY]: "productIntroduction",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 33,
  },
  {
    menuId: 34,
    [MENU_TITLE]: "注意事项",
    [MENU_KEY]: "mattersNeedingAttention",
    [MENU_PARENTKEY]: "productIntroduction",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 34,
  },
  {
    menuId: 35,
    [MENU_TITLE]: "版本",
    [MENU_KEY]: "version",
    [MENU_PARENTKEY]: "productIntroduction",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 35,
  },
  {
    menuId: 4,
    [MENU_TITLE]: "运维文档",
    [MENU_KEY]: "OperationAndMaintenanceDocuments",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 4,
  },
  {
    menuId: 41,
    [MENU_TITLE]: "Aliyun源使用帮助",
    [MENU_KEY]: "Aliyun",
    [MENU_PARENTKEY]: "OperationAndMaintenanceDocuments",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 41,
  },
  {
    menuId: 42,
    [MENU_TITLE]: "CentOS源使用帮助",
    [MENU_KEY]: "CentOS",
    [MENU_PARENTKEY]: "OperationAndMaintenanceDocuments",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 42,
  },
  {
    menuId: 43,
    [MENU_TITLE]: "EPEL源使用帮助",
    [MENU_KEY]: "EPEL",
    [MENU_PARENTKEY]: "OperationAndMaintenanceDocuments",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 43,
  },
  {
    menuId: 44,
    [MENU_TITLE]: "Pypi源使用帮助",
    [MENU_KEY]: "Pypi",
    [MENU_PARENTKEY]: "OperationAndMaintenanceDocuments",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 44,
  },
  {
    menuId: 45,
    [MENU_TITLE]: "添加静态路由",
    [MENU_KEY]: "AddStaticRoute",
    [MENU_PARENTKEY]: "OperationAndMaintenanceDocuments",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 45,
  },
  {
    menuId: 5,
    [MENU_TITLE]: "运维文档",
    [MENU_KEY]: "OtherDocuments",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 5,
  },
];
let menu1: MenuList = [
  {
    menuId: 2,
    [MENU_TITLE]: "存储介绍",
    [MENU_KEY]: "introduce",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 2,
  },
  {
    menuId: 5,
    [MENU_TITLE]: "运维文档",
    [MENU_KEY]: "OtherDocuments",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 5,
  },
];
let menu3: MenuList = [
  {
    menuId: 2,
    [MENU_TITLE]: "容器介绍",
    [MENU_KEY]: "introduce",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 2,
  },
  {
    menuId: 5,
    [MENU_TITLE]: "运维文档",
    [MENU_KEY]: "OtherDocuments",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 5,
  },
];
let menu4: MenuList = [
  {
    menuId: 2,
    [MENU_TITLE]: "云数据库介绍",
    [MENU_KEY]: "introduce",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 2,
  },
  {
    menuId: 5,
    [MENU_TITLE]: "运维文档",
    [MENU_KEY]: "OtherDocuments",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 5,
  },
];
let menu5: MenuList = [
  {
    menuId: 2,
    [MENU_TITLE]: "运维服务介绍",
    [MENU_KEY]: "introduce",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 2,
  },
  {
    menuId: 5,
    [MENU_TITLE]: "运维文档",
    [MENU_KEY]: "OtherDocuments",
    [MENU_PARENTKEY]: "",
    [MENU_ICON]: "",
    [MENU_KEEPALIVE]: "false",
    order: 5,
  },
];

let menus = {
  'calculation':  menu,
  'storage':  menu1,
  'container':  menu3,
  'cloudDatabase':   menu4,
  'OMS':  menu5,
}

const userInfo = {
  msg: "登录成功",
  status: 0,
  token: "12323",
  data: {
    // user_id: 1,
    // username: "超级管理员",
    // account: "admin",
    // type: "0",
    // isLogin: true,
    isDocAdmin:false,
    username:'能解决',
    employ_id:8,
    department:'XXXX'
  },
};

const doc:DocInfoResponse={
  msg: "",
  status: 0,
  data: {
    content:'',
    uuid:'',
    docId:'PlatformOverview',
    nextId:'OtherDocuments',
    nextTitle:'运维文档',
    prevId:'',
    prevTitle:'',
  }
}
const doc2:DocInfoResponse={
  msg: "",
  status: 0,
  data: {
    content:'',
    uuid:'',
    docId:'PlatformOverview',
    nextId:'QuickUse',
    nextTitle:'快速使用',
    prevId:'introduce',
    prevTitle:'计算介绍',
  }
}


const evaluate = {
  msg: "感谢您的评价",
  status: 0,
};

const editDocContent = {
  msg: "修改成功!",
  status: 0,
};

const menuData={
  status:0,
  data: {
    menus:[]
  }
}


const MockData: MockDataType = {
  "/getMenu/{productType}": {...menuData,status:0,docs:'',data:{menus:formatMenu(menus.calculation)}},
  "/login": userInfo,
  '/getDocDetail/{doc_id}': doc,
  "/evaluate":evaluate,
  "/editDocContent":editDocContent
};

type UrlType = keyof MockDataType
function get(url: UrlType, data:any) {
  return new Promise((res) => {
    setTimeout(() => {
      let myUrl = url as string;
      if(myUrl.split('/')[1]==='getMenu'){
        MockData[url] = {...menuData,data:{menus:formatMenu(menus[myUrl.split('/')[2] as keyof typeof menus])}};
        res(MockData[url]);
        return;
      }
      if (myUrl.split('/')[1]==='getDocDetail'){
        switch(myUrl.split('/')[2]){
          case "introduce":
            return res({...doc,data:{...doc.data,content:readFile('mock/test.md')}});
          case "CalculationUse":
            return res({...doc,data:{...doc.data,content:readFile('mock/test2.md')}});
          default:
            let response = {...doc2,data:{...doc2.data,content:readFile('mock/data.md')}}
            return res(response)
        }
      }
      if(myUrl.indexOf('evaluate')>-1){        
        return res(MockData['/evaluate']);
      }
    }, 500);
  }).then((res) => {
    if (res) {
      return res
    } else {
      message.error("接口暂未配置")
      return Promise.reject("接口暂未配置")
    }
  });
}



function post(url: UrlType, data: any) {
  return new Promise((res) => {
    setTimeout(() => {
      switch (url) {
        case "/login":
          // userInfo.data.account = data.account;
          if (data.account.indexOf("admin") === -1) {
            // userInfo.data.type = "1";
            // userInfo.data.username = "普通用户";
          }
          return res(userInfo);
        case '/evaluate':
          return res(MockData[url]);
        case '/editDocContent':
          doc.data.content=data
          return res(MockData[url]);
        default:
          res({ status: 1, msg: "暂无" });
          break;
      }
    }, 100);
  }).then((res: any) => {
    if (res.status === 0) {
      return res
    } else {
      message.error("接口暂未配置")
      return Promise.reject("接口暂未配置")
    }
  });
}

function readFile(name: string) { // name为文件所在位置
  let xhr = new XMLHttpRequest(),
  okStatus = document.location.protocol === "file:" ? 0 : 200;
  xhr.open('GET', 'http://localhost:3000/src/'+name, false);
  xhr.overrideMimeType("text/html;charset=utf-8");//默认为utf-8
  xhr.send(null);
  return xhr.status === okStatus ? xhr.responseText : null;
}


const mock = { get, post };

export default mock;
